package com.example.memosystem.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.memosystem.R;
import com.example.memosystem.activity.listen.ChangeListen;
import com.example.memosystem.activity.listen.TouchListen;
import com.example.memosystem.dao.impl.AdminDao;

import java.util.Map;
import java.util.Objects;

public class MesActivity extends AppCompatActivity {

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_mes);

        Intent intent = getIntent();
        String account = intent.getStringExtra("account");

        Map<String, String> result = new AdminDao().showAccountMesAdmin(account);

        EditText name = findViewById(R.id.change_name);
        name.setText(result.get("name"));
        RadioButton man = findViewById(R.id.change_man);
        RadioButton woman = findViewById(R.id.change_woman);
        if(Objects.equals(result.get("sex"), "男"))
        {
            man.setChecked(true);
        }else{
            woman.setChecked(true);
        }

        EditText phone = findViewById(R.id.change_phone);
        phone.setText(result.get("phone"));
        EditText age = findViewById(R.id.change_age);
        age.setText(result.get("age"));

        name.addTextChangedListener(new ChangeListen(name));
        phone.addTextChangedListener(new ChangeListen(phone));
        age.addTextChangedListener(new ChangeListen(age));


        name.setOnTouchListener(new TouchListen(name));
        phone.setOnTouchListener(new TouchListen(phone));
        age.setOnTouchListener(new TouchListen(age));
        //初始状态
        name.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
        phone.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
        age.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);


        Button button = findViewById(R.id.change_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nameT = name.getText().toString().trim();
                String ageT = age.getText().toString().trim();
                String phoneT = phone.getText().toString().trim();
                if(nameT.isEmpty())
                {
                    Toast.makeText(MesActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
                }else if(phoneT.isEmpty()){
                    Toast.makeText(MesActivity.this, "请输入电话", Toast.LENGTH_SHORT).show();
                }else if(ageT.isEmpty()){
                    Toast.makeText(MesActivity.this, "请输入年龄", Toast.LENGTH_SHORT).show();
                }else{
                    String sex = "女";
                    if(man.isChecked())
                    {
                        sex = "男";
                    }
                    AdminDao adminDao = new AdminDao();
                    int a = adminDao.changeMesAdmin(account, nameT, sex, phoneT, ageT);
                    if(a == 0)
                    {
                        Toast.makeText(MesActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }else if(a >= 1)
                    {
                        Toast.makeText(MesActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MesActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
                    }


                }

            }
        });

        //菜单
        Toolbar toolbar = findViewById(R.id.toolbar_mes);
        this.setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MesActivity .this, ManageActivity.class);
                intent1.putExtra("account", account);
                startActivity(intent1);
            }
        });

    }

}